package net.sx.minemodule.widget;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.homegoods.classify.model.Constant;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.BaseItem;
import com.kedll.library.util.BindingUtil;

import net.sx.minemodule.R;
import net.sx.minemodule.databinding.MineItemMineOrdersBinding;
import net.sx.minemodule.mvp.bean.GoodsBean;
import net.sx.minemodule.mvp.bean.OrderBean;

import java.util.List;

/**
 * 描述:  订单 item 多个商品
 * author:胡邦茂
 * CreateDate: 2019/7/7 11:30
 */

public class MineOrderGoodViews extends LinearLayout {

    private MineItemMineOrdersBinding mBinding;

    private MultiTypeAdapter adapter = new MultiTypeAdapter();
    private OrderBean mModel;

    public MineOrderGoodViews(Context context) {
        this(context, null);
    }

    public MineOrderGoodViews(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MineOrderGoodViews(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.mine_item_mine_orders, this, true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        mBinding.listMineOrder.setLayoutManager(layoutManager);
        mBinding.listMineOrder.setNestedScrollingEnabled(false);
        mBinding.listMineOrder.setHasFixedSize(true);
        mBinding.listMineOrder.setAdapter(adapter);
    }

    public void setData(OrderBean model) {
        mModel = model;
        setHeadImage(Constant.SERVER_URL + model.getShopImg());
        setStoreName(model.getStopName());
        setOrderType(model.getOrderType());

        //1、订单的商品
        List<GoodsBean> dataList = model.getOrderProducts();
        adapter.clear();
        for (GoodsBean bean : dataList) {
            BaseItem item = bean.createItem(adapter);
            adapter.addItem(item);
        }

        //2、设置其他信息
        setAllCount(model.getGoodsCount());
        setAllMoney(String.valueOf(model.getTotalAmount()));
    }


    /**
     * 设置头像
     *
     * @param headUrl
     */
    private void setHeadImage(String headUrl) {
        BindingUtil.loadCircleImage(mBinding.ivStoreImgMineOrder, headUrl, getContext().getResources().getDrawable(R.drawable.load_error));

    }

    /**
     * 设置店铺名
     *
     * @param storeName
     */
    private void setStoreName(String storeName) {
        mBinding.tvStoreNameMineOrder.setText(storeName);
    }

    /**
     * 订单类型
     *
     * @param orderType
     */
    private void setOrderType(String orderType) {
        mBinding.tvOrderType.setText(orderType);
    }

    /**
     * 共计商品
     *
     * @param allGoods
     */
    private void setAllCount(int allGoods) {
        mBinding.tvNum.setText(String.format("共%d件", allGoods));
    }

    /**
     * 合计
     *
     * @param money
     */
    private void setAllMoney(String money) {
        SpannableString s = new SpannableString("¥ " + money);
        //缩小0.7
        RelativeSizeSpan start = new RelativeSizeSpan(0.7f);
        s.setSpan(start, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        int index = s.toString().indexOf(".");
        if (index != -1) {
            s.setSpan(start, index, s.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        mBinding.tvMoney.setText(s);
    }

}
